<?php
declare(strict_types=1);

namespace App\Listener;

use App\Event\Web\ArticlePublishEvent;
use App\Job\PublishArticleJob;
use App\Service\QueueService;
use App\Service\Web\ArticleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\Event\Annotation\Listener;
use Hyperf\Event\Contract\ListenerInterface;

#[Listener]
class ArticlePublishListener implements ListenerInterface
{
    #[Inject]
    private QueueService $queueService;

    #[Inject]
    private ArticleService $articleService;

    public function listen(): array
    {
        return [
            ArticlePublishEvent::class,
        ];
    }

    /**
     * @param ArticlePublishEvent $event
     * @return void
     */
    public function process(object $event)
    {
        $this->queueService->push(
            new PublishArticleJob($event->articleId, $this->articleService),
            $event->publishTime->getTimestamp() - time()
        );
    }
}